package com.jiazhong.basic.pack0724;

import lombok.SneakyThrows;

import java.io.*;
import java.util.Arrays;

// 文件的读取（字节流形式）
public class DemoC {

    // 一次读取一个字节
    @SneakyThrows
    private static void a() {
        File file = new File("/Users/dailyblue/workspace/io/2.txt");
        if (!file.exists()) {
            System.out.println("文件不存在");
            return;
        }
        if (file.isDirectory()) {
            System.out.println("读取的必须是文件");
            return;
        }
        InputStream in = new FileInputStream(file);
        // 读取了一个字
        int read = in.read();
        System.out.println(read);
        in.close();
    }

    // 根据循环将内容读取完毕
    @SneakyThrows
    private static void b() {
        File file = new File("/Users/dailyblue/workspace/io/2.txt");
        if (!file.exists()) {
            System.out.println("文件不存在");
            return;
        }
        if (file.isDirectory()) {
            System.out.println("读取的必须是文件");
            return;
        }
        InputStream in = new FileInputStream(file);
        while (true) {
            // 读取了一个字
            int read = in.read();
            if (read == -1) {
                break;
            }
            System.out.print((char) read);
        }
        in.close();
    }

    // 一次读取多个字节
    @SneakyThrows
    private static void c() {
        File file = new File("/Users/dailyblue/workspace/io/2.txt");
        if (!file.exists()) {
            System.out.println("文件不存在");
            return;
        }
        if (file.isDirectory()) {
            System.out.println("读取的必须是文件");
            return;
        }
        InputStream in = new FileInputStream(file);
        // 一次读取了十个字节
        byte[] buffer = new byte[19];
        // len:实际读取的长度
        int len = in.read(buffer);
        if (len == -1) {
            System.out.println("没有内容");
        }
        System.out.println(Arrays.toString(buffer));
        String str = new String(buffer);
        System.out.println(str);
        in.close();
    }

    @SneakyThrows
    private static void d() {
        File file = new File("/Users/dailyblue/workspace/io/2.txt");
        if (!file.exists()) {
            System.out.println("文件不存在");
            return;
        }
        if (file.isDirectory()) {
            System.out.println("读取的必须是文件");
            return;
        }
        InputStream in = new FileInputStream(file);
        // 一次读取了十个字节
        byte[] buffer = new byte[10];
        // len:实际读取的长度
        while (true) {
            int len = in.read(buffer);
            if (len == -1) {
                System.out.println("没有内容");
                break;
            }
            String str = new String(buffer);
            System.out.print(str);
        }
        in.close();
    }


    // 将5.png复制到另一个目录中
    @SneakyThrows
    private static void e() {
        File srcFile = new File("/Users/dailyblue/workspace/io/a/5.png");
        File destFile = new File("/Users/dailyblue/workspace/io/b/5.png");
        // 读取文件
        InputStream in = new FileInputStream(srcFile);
        // 写入文件
        OutputStream out = new FileOutputStream(destFile);
        byte[] buffer = new byte[1024];
        while (true) {
            int len = in.read(buffer);
            if (len == -1) {
                break;
            }
            out.write(buffer, 0, len);
        }
        in.close();
        out.close();
    }

    public static void main(String[] args) {
        e();
    }
}
